package com.school.repair.controller;

import com.google.zxing.WriterException;
import com.school.repair.entity.*;
import com.school.repair.service.*;
import com.school.repair.util.AjaxResult;
import com.school.repair.util.Page;
import com.school.repair.util.QRCodeUtil;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Map;

@RestController
public class BaoXiuManController {
    @Resource
    BaoXiuManService bxms;
    @Resource
    EquipmentService es;

    @Resource
    RepairManService rs;

    @Resource
    BaoXiuDanService bxds;

    @Resource
    TodoService ts;

    @Resource
    CommentService cs;

    @Resource
    EquipmentPicService equipmentPicService;


    @RequestMapping(value = "Baoxiumanlogin", produces = "application/json;charset=utf-8")
    protected AjaxResult login(BaoXiuMan baoXiuMan, HttpSession session) {
        String name = baoXiuMan.getCname();
        String pass = baoXiuMan.getPass();
        BaoXiuMan bxm = bxms.login(name, pass);
        if (bxm != null) {
            session.setAttribute("login", "666");
            return AjaxResult.success(bxm);
        }
        return AjaxResult.error("用户名或者密码不正确！");
    }

    @RequestMapping(value = "equipmentPaging", produces = "application/json;charset=utf-8")
    protected Page<Equipment> equipmentPaging(@RequestParam(value = "page", defaultValue = "1") int pageNo, @RequestParam(value = "limit", defaultValue = "10") int pageSize, @RequestParam(value = "createdate", required = false) Date createdate, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "id", defaultValue = "0") long id) {
        return es.equipmentPaging(pageNo, pageSize, createdate, name, id);
    }

    @RequestMapping(value = "equipmentSearch", produces = "application/json;charset=utf-8")
    protected Page<Equipment> equipmentSearch(@RequestParam(value = "page", defaultValue = "1") int pageNo, @RequestParam(value = "limit", defaultValue = "10") int pageSize, @RequestParam(value = "equipmentType", defaultValue = "0") long equipmentType, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "id", defaultValue = "0") long id)
            throws ServletException, IOException {
        return es.equipmentSearch(pageNo, pageSize, equipmentType, name, id);
    }

    @RequestMapping(value = "baoxiumanAddEquipment", produces = "application/json;charset=utf-8")
    protected AjaxResult baoxiumanAddEquipment(Equipment equipment) {
        int r = 0;
        try {
            r = es.unitAddEquipment(equipment);
        } catch (Exception e) {
        }
        if (r == 0) {
            return AjaxResult.error("添加设备失败");
        }
        return AjaxResult.success("添加设备成功");
    }

    //  拿到所有的工程师并进行分页
    @RequestMapping(value = "repairManserch", produces = "application/json;charset=utf-8")
    protected Page<RepairMan> repairManserch(@RequestParam(value = "page", defaultValue = "1") int pageNo, @RequestParam(value = "limit", defaultValue = "10") int pageSize, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "goodat", required = false) String goodat) {
        return rs.repairManPage(pageNo, pageSize, name, goodat);
    }

    //接收保修单请求
    @RequestMapping(value = "dorepair", produces = "application/json;charset=utf-8")
    protected AjaxResult dorepair(BaoXiuDan baoXiuDan) {
        int r = 0;
        try {
            r = bxds.add(baoXiuDan);
        } catch (Exception e) {
        }
        if (r == 0) {
            return AjaxResult.error("申报失败");
        }
        //添加一条 TODO数据
        Todo td = new Todo();
        td.setContent(baoXiuDan.getContent());
        td.setEquipmentId(baoXiuDan.getEquipmentid());
        td.setRepairmanid(baoXiuDan.getRepairmanid());
        ts.add(td);
        return AjaxResult.success("申报成功");
    }

    //生成二维码
    @RequestMapping(value = "addQrcode", produces = "application/json;charset=utf-8")
    protected AjaxResult addQrcode(@RequestParam("id") long id, HttpSession session) throws IOException, WriterException {
        //1.创建一个文件流
        ServletContext application = session.getServletContext();
        OutputStream output = new FileOutputStream(application.getRealPath("images") + "/" + id + ".jpg");
        QRCodeUtil.genGR("https://dhrweb.utools.club/equipmentUpdate.html?" + id, output);
        String barCode = id + "jpg";
        int a = es.addBarCode(id, barCode);
        if (a > 0) {
            return AjaxResult.success("生成成功");
        }
        return AjaxResult.error("生成失败");
    }

    //添加一条反馈
    @RequestMapping(value = "addcomment", produces = "application/json;charset=utf-8")
    protected AjaxResult addcomment(@RequestParam("baoxiudanId") long baoxiudanId, @RequestParam("fee") double fee, @RequestParam("score") double score, @RequestParam("repairRes") String repairres, @RequestParam("content") String content) {
        Comment c = new Comment();
        c.setContent(content);
        c.setBaoxiudanId(baoxiudanId);
        c.setScore(score);
        int r = 0;
        try {
            r = bxds.update(baoxiudanId, repairres, fee);
            r = cs.add(c);
        } catch (Exception e) {
        }
        if (r != 0) {
            return AjaxResult.success("反馈成功");
        }
        return AjaxResult.error("反馈失败");
    }


    @RequestMapping(value = "getEquipmentPic", produces = "application/json;charset=utf-8")
    protected AjaxResult getEquipmentPic(@RequestParam("id") long id) {
        EquipmentPic equipmentPic = equipmentPicService.getEquipmentPic(id);
        if (equipmentPic != null)
            return AjaxResult.success(equipmentPic);
        return AjaxResult.error("此设备无照片");
    }

    @RequestMapping(value = "addEQPic", produces = "application/json;charset=utf-8")
    protected AjaxResult addEQPic(EquipmentPic equipmentPic) {
        equipmentPicService.delPic(equipmentPic.getEquipmentId());
        int r = equipmentPicService.add(equipmentPic);
        if (r > 0) {
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("此设备无照片");
    }

    @RequestMapping(value = "addpic", produces = "application/json;charset=utf-8")
    public Map<String, Object> addpic(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        Assert.notNull(file, "上传文件不能为空");
        String filepath = request.getServletContext().getRealPath("/img");
        String filename = System.currentTimeMillis() + file.getOriginalFilename();
        File file2 = new File(filepath);
        if (!file2.exists()) {
            file2.mkdirs();
        }
        String savepath = filepath + "\\" + filename;
        try {
            file.transferTo(new File(savepath));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("上传失败");
        }
        return AjaxResult.success(savepath);
    }

}
